/*
*  Copyright 2019-2020 Liu
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.liu.modules.business.rest;


import com.liu.annotation.AnonymousAccess;
import com.liu.modules.business.domain.EljAdvertising;
import com.liu.modules.business.service.EljAdvertisingService;
import com.liu.modules.business.service.dto.EljAdvertisingQueryCriteria;
import com.liu.annotation.Log;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://el-admin.vip
* @author Liu
* @date 2022-07-04
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "广告管理管理")
@RequestMapping("/api/eljAdvertising")
public class EljAdvertisingController {

    private final EljAdvertisingService eljAdvertisingService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('eljAdvertising:list')")
    public void download(HttpServletResponse response, EljAdvertisingQueryCriteria criteria) throws IOException {
        eljAdvertisingService.download(eljAdvertisingService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询广告管理")
    @ApiOperation("查询广告管理")
    @AnonymousAccess
    public ResponseEntity<Object> query(EljAdvertisingQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(eljAdvertisingService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @ApiOperation("上传广告")
    @PostMapping(value = "/upload")
    @PreAuthorize("@el.check('eljAdvertising:add')")
    public ResponseEntity<Object> create(@RequestParam String name,@RequestParam String advertisCompany,String advertisUrl,String advertisEndTime, @RequestParam("file") MultipartFile file){
        eljAdvertisingService.upload(name,advertisCompany,advertisUrl,advertisEndTime,file);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }


//    @PostMapping
//    @Log("新增广告管理")
//    @ApiOperation("新增广告管理")
//    @PreAuthorize("@el.check('eljAdvertising:add')")
//    public ResponseEntity<Object> create(@Validated @RequestBody EljAdvertising resources){
//        return new ResponseEntity<>(eljAdvertisingService.create(resources),HttpStatus.CREATED);
//    }

    @PutMapping
    @Log("修改广告管理")
    @ApiOperation("修改广告管理")
    @PreAuthorize("@el.check('eljAdvertising:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody EljAdvertising resources){
        eljAdvertisingService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除广告管理")
    @ApiOperation("删除广告管理")
    @PreAuthorize("@el.check('eljAdvertising:del')")
    @DeleteMapping
    public ResponseEntity<Object> delete(@RequestBody String[] ids) {
        eljAdvertisingService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}